<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/mb/jsp/common.jsp" %>
<%@ page import="com.wsu.search.tools.Tools" %>
<%@ page import="com.limao.DBDataClass.CrowdSysProjectClass" %>
<%@ page import="com.limao.CacheManager.CrowdSysProjectCache" %>
<%@ page import="com.alibaba.fastjson.JSON" %>

<%
    //取得url带过来的参数
    String projectid = Tools.RmNull(request.getParameter("projectid"));
    CrowdSysProjectClass crowd = CrowdSysProjectCache.getInstance().load_Goods_Info_ById(CacheClass.getIntValue(projectid));
    
     String countNum = Tools.RmNull(request.getParameter("countNum"));
     
      String selectextraVal = Tools.RmNull(request.getParameter("selectextraVal"));
      
      
      
      
      
      String kind = Tools.RmNull(request.getParameter("kind"));/* 0：详情处下单，1：购物车处下单 */
      
      String shoppingCarPriceAll = Tools.RmNull(request.getParameter("priceAll"));/*购物车总价*/
      
       String projectidArr = Tools.RmNull(request.getParameter("projectidArr"));
       
       String countNumArr = Tools.RmNull(request.getParameter("countNumArr"));
       
       String guigeArr = Tools.RmNull(request.getParameter("guigeArr"));
     
/*     String address = Tools.RmNull(request.getParameter("address"));
    address = new String(address.getBytes("iso-8859-1"),"utf-8"); */
    
    
/*     String shengid = Tools.RmNull(request.getParameter("shengid"));
    String cityid = Tools.RmNull(request.getParameter("cityid")) */;

%>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品下单</title>
     <!-- 引用jquery.cookie.js -->
    <script src="mb/js/jquery.cookie.js"></script>
</head>
<body  ontouchstart="">
<div class="detailTop pf w100 of">
    <div class="w100 of tc cf f18">
        <span class="pa" style="left: 2px;" onclick="javascript:history.back();"><img src="mb/images/top_back_white.png" style="margin-top: 4px;height: 40px;width: 40px;"/></span>
         商品下单
     </div>
</div>

<div class="container jz mgt45">
	<%if(kind.equals("0")){%>
		    <section class="proSection of w100 bcf mgt10">
	      <div class="w100 w92 jz lh2 mgt10 b-b">
	       	<div class="w100 of flexBox">
	       	   <p class="tl f16 c3">单价：<span class="orange2" id="pgprice">&yen;<%=crowd.packagepriceAbout %></span></p>
	       	   <%if(crowd.viplevel.equals("0")){ %>
	                     	<p class="tr f14 c3">送代理天数：无</p>
	           <%} else{%>
	           		<p class="tr f14 c3">送代理天数：送VIP<%=crowd.viplevel %>级：<%=crowd.days %>天</p>
	           <%}%>
	       	   
	       	</div>
		      <div class="w100 of flexBox lh2 mgb10">
			       <% if (Double.valueOf(crowd.frieght) == 0) {  %>
		             <p class="tl f14 c3">配送费：<span class="orange2" id="pgprice">免费</span></p>
		          <%}else { %>
		           <p class="tl f14 c3">配送费：<span class="orange2" id="pgprice">&yen;<%=crowd.frieght%></span></p>
		          <%}%>
		          <p class="tr f14 c3">数量：	&times;  <%=countNum %></p>
		       </div>
	      </div>
	      <div class="w100 f15 c3 w92 jz lh2 tr">
	      		<%
	      			 int num = Integer.parseInt(countNum);
	      			double singlePrice = Double.parseDouble(crowd.packagepriceAbout);
	      			double priceTotal = num * singlePrice;
	      		 %>
	      		实付金额总计：<span class="orange2" id="pgprice">&yen;<%=priceTotal %></span>
	      </div>
    </section>
	<%}else{%>
		 <div class="w100 f15 c3 w92 jz lh2 tr">
	      		实付金额总计：<span class="orange2" id="pgprice">&yen;<%=shoppingCarPriceAll %></span>
	      </div>
	<%}%>


    
    <section class="proSection pr w100 bcf mgt10" >
       <section id="supportMoney" class="w100 db of" >
              <h4 class="c3 f16 mgt10 w92 jz lh2" style="border:none;">支付方式：</h4>
              <section id="supportMoney" class="supportMoney of clearfix w92 jz f15">
                  <div class=" of clearfix b-b">
                        <img src="mb/images/weixin_Pay.png" class="fl"/><p class="fl mgl10">微信支付</p><span class="circle_check fr"></span>
                  </div>
                  <div class="of clearfix b-b">
                         <img src="mb/images/zhifubao_Pay.png" class="fl"/>	<p class="fl mgl10">支付宝支付</p><span class="circle_check fr"></span>
                  </div>
                   <div class="of clearfix supportMoney_current">
                        <img src="mb/images/yue_Pay.png" class="fl"/><p class="fl mgl10">余额支付</p><span class="circle_check fr"></span>
                  </div>
              </section>
          </section>
      	 <form id="buyForm" name="buyForm" method="post" action="PcWebPayServlet"></form>
    </section>
    
     <section class="proSection w100 bcf mgt10" >
	     <div class="w92 jz of">
	     	<h4 class="c3 f16 mgt10  jz lh2" style="border:none;">收货地址：</h4>
	     	<section id="city_3" class="addressFlex df of f15 c3 b-b">
	     		<p>省市</p>
	       		 <div id="selectbox" class="content-block" style="margin-left:16px;">
					
				</div> 
		    </section>

		    <section class="addressFlex df of f15 c3  b-b">
		    	<p>详细地址</p>
		        <div class="addressBlock">
		             <input type="text" name="detailAddress" value="" placeholder="请输入详细地址" required="required" id="detailAddress"
		                   class="addressInput"/>
		        </div>
		    </section>
		    <section class="addressFlex df of f15 c3  b-b">
		        <p>联系人</p>
		        <div class="addressBlock">
		            <input type="text" name="username" value="" placeholder="请输入联系人" required="required" id="username"
		                   class="addressInput"/>
		        </div>
		    </section>
		    <section class="addressFlex df of f15 c3  b-b">
		        <p>联系电话</p>
		        <div class="addressBlock">
		             <input type="tel" name="usertel" value="" placeholder="请输入联系电话" required="required" id="usertel"
                   class="addressInput"/>
		        </div>
		    </section>
		    
		    
	     </div>
     </section>
     

     
     
 
    <section class="w100 of">
        <div class="w100 tc f12">
<!--             <input type="checkbox" name="agreement" checked="checked" id="agreement" />
            <label for="agreement" class="tc f12 c9">我已阅读并同意七彩云购的 <span style="color: #0066ff;">《支持者协议》</span></label> -->
        </div>
        <div class="w100">
            <button type="button" onclick="pressOrder()" id="payBtn" class="btnBlue f16 cf tc db w100">支&nbsp;付</button>
        </div>
        <p class="tc f12 c9">
            请您在 <span class="cred">24小时</span> 内付清款项，否则订单会被自动取消并删除
        </p>
    </section>
    </div>

</body>


        <!--  引入城市选择相关文件  -->
    <link rel="stylesheet" href="mb/css/LArea.css"/>
    <script src="mb/js/LArea/LAreaData1.js"></script>
	<script src="mb/js/LArea/LAreaData2.js"></script>
    <script src="mb/js/LArea/LArea.js"></script>
       <!--  引入城市选择相关文件-End  -->
       
<script type="text/javascript" src="mb/js/websql.js"></script>       
<script>
    var paytype = ""; // 支付方式(0支付宝，1微信，2到付(不用),3余额)
    var selectextraVal = "";
    
    //详细地址过滤下划线等符号
	function Test_address(detailAddress) { 
		var myReg = /^[^_\x22]+$/;
		if (myReg.test(detailAddress)){ return true;}else{ return false; }
	}
	
	
/* 	var myDetailAddress ="";
	var myUsertel ="";
	var myUsername ="";
	var myProvCity = "";
	var myProvCityId=""; */
	
	
   //获取url中的参数
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg);  //匹配目标参数
            if (r != null) return unescape(r[2]); return null; //返回参数值
        }
        var projectidArr =[];
    	 projectidArr = getUrlParam('projectidArr');
	

		
    $(function () {
    	var selectStr="";
    	var myProCityVal = "";
    	var myProCityIdVal = "";
    	 $.ajax({
            url: "PlugServlet",
            data: JSON.stringify({m: 146}),
            type: 'post',
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            beforeSend: function () {
            },
            success: function (data) {
                console.log(data);
                if (data.state == 1) {
                	myProCityIdVal = data.obj.shengid +","+data.obj.cityid;
                     // 设置默认地址
                        if (typeof(data.obj.address) != "undefined") {
                        console.log("取上次的收货地址");
	                        // 设置默认地址
	                        $.each(provs_data, function (i, prov) {
	                        	if(prov.value == data.obj.shengid){
	                        		myProCityVal= provs_data[i].text+",";
	                        		 return true;
	                        	}
	                        
						     });
						      $.each(citys_data, function (i, cityBlcok) {
						      	$.each(cityBlcok, function (i, city){
		                        	if(city.value == data.obj.cityid){
		                        		console.log(cityBlcok[i]);
		                        		myProCityVal=myProCityVal + cityBlcok[i].text;
		                        		 return true;
		                        	}
						      	});
						      
						     });
						     selectStr = '<input id="demo2" type="text" readonly="" placeholder="省市城市选择" class="addressInput"  value="'+myProCityVal+'"/>'
										+'<input id="value2" type="hidden"  class="addressInput" value="'+myProCityIdVal+'"/>';                    
	                    }else{
	                    	selectStr = '<input id="demo2" type="text" readonly="" placeholder="请选择省份城市" />'
									+'<input id="value2" type="hidden" />';
	                    }
	                    $("#selectbox").html(selectStr);
	                    //调用城市选择
	                    proCityChoose();
	                    
	                    $("#detailAddress").val(data.obj.address);//写入详细地址
	                    $("#usertel").val(data.obj.tel);//写入电话
	                    $("#username").val(data.obj.person_name);//写入联系人

                } else {
                    // 其他错误弹出提示
                    layer.alert(data.info);
                }
            },
            error: function (data) {
                layer.alert(data.info);
            },
            complete: function () {
            }
        });
        
          //调用城市选择
	      proCityChoose();
    
    
        //取上次的收货地址
       /*  if ($.cookie("detailAddressCookie") != undefined && $.cookie("detailAddressCookie") !="") {
			$("#detailAddress").val( $.cookie("detailAddressCookie"));//写入详细地址
			} 
		   myDetailAddress=$.cookie("detailAddressCookie");
		   
		   
		    if ($.cookie("usertelCookie") != undefined && $.cookie("usertelCookie") !="") {
			$("#usertel").val( $.cookie("usertelCookie"));//写入电话
			} 
		   myUsertel=$.cookie("usertelCookie");
		   
		   
		    if ($.cookie("usernameCookie") != undefined && $.cookie("usernameCookie") !="") {
			$("#username").val( $.cookie("usernameCookie"));//写入联系人
			} 
		   myUsername=$.cookie("usernameCookie");
		   
		   
		   	var selectStr="";
	       if ($.cookie("provCityCookie") != undefined && $.cookie("provCityCookie") !="") {
	       		if($.cookie("provCityIdCookie") != undefined||$.cookie("provCityIdCookie") !=""){
	       			selectStr = '<input id="demo2" type="text" readonly="" placeholder="省市城市选择" class="addressInput"  value="'+$.cookie("provCityCookie")+'"/>'
									+'<input id="value2" type="hidden"  class="addressInput" value="'+$.cookie("provCityIdCookie")+'"/>';
	       			
	       		}
			} else{
				selectStr = '<input id="demo2" type="text" readonly="" placeholder="请选择省份城市" />'
									+'<input id="value2" type="hidden" />';
			
			}
			$("#selectbox").html(selectStr);
		   myProvCity=$.cookie("provCityCookie");
		   myProvCityId=$.cookie("provCityIdCookie"); */
		   
     		//取上次的收货地址--END

		   
		  /*  
		   	var selectStr="";
	       if ($.cookie("provCityCookie") != undefined && $.cookie("provCityCookie") !="") {
	       		if($.cookie("provCityIdCookie") != undefined||$.cookie("provCityIdCookie") !=""){
	       			selectStr = '<input id="demo2" type="text" readonly="" placeholder="省市城市选择" class="addressInput"  value="'+$.cookie("provCityCookie")+'"/>'
									+'<input id="value2" type="hidden"  class="addressInput" value="'+$.cookie("provCityIdCookie")+'"/>';
	       			$("#selectbox").html();
	       		}
			} else{
				selectStr = '<input id="demo2" type="text" readonly="" placeholder="请选择省份城市" />'
									+'<input id="value2" type="hidden" />';
			
			}
		   myProvCity=$.cookie("provCityCookie");
		   myProvCityId=$.cookie("provCityIdCookie"); */

    
			

                
        /**
         * 支付方式选择
         */
         
          //切换支付方式
        $('#supportMoney>div').click(function () {
        
        	$(this).addClass("supportMoney_current").siblings().removeClass("supportMoney_current");

        });
       
        
    });
    
    /*
    *	城市选择
    */
    
    function proCityChoose(){
    
    		var area2 = new LArea();
			area2.init({
				'trigger': '#demo2',
				'valueTo': '#value2',
				'keys': {
					id: 'value',
					name: 'text'
				},
				'type': 2,
				'data': [provs_data, citys_data]
			});
			/* area2.value=[110000,110100];//控制初始位置，注意：该方法并不会影响到input的value */
    }
      		

    /**
     * 下单
     */
    function pressOrder() {
    
    
/*         if (!document.getElementById("agreement").checked) {
            showToast("请先同意支持者协议！");
            return;
        } */
        //后台已经做判断，不用管支付方式
        
       /*  if (paytype < 2) {
            showToast("暂不支持，请选择  其它支付！");
            return false;
        } */
        
      
  <%--     
        contentHtml=contentHtml+'<input type="hidden" name="projectid" id="projectid" value="'+projectidV+'"/>'+
								'<input type="hidden" name="selectextra" id="selectextra" value="'+selectextraVal+'"/>'+
 								'<input type="hidden" name="num" id="num" value="'+numVal+'"/>'+
 								'<input type="hidden" name="about" id="about" value=""/>'+ 
 								'<input type="hidden" name="paytype" id="paytype" value="'+paytype+'"/>';       
        $("#buyForm").html(contentHtml);
        document.buyForm.submit();
 --%>  
       
/*         if(paytype*1.0==2 || paytype*1.0==4)
		{
		   var tempwindow=window.open();
		} */
		
		
		
			var txt = $.trim( $("#supportMoney .supportMoney_current").text());
             console.log(txt);
            if (txt == '支付宝支付') {
                paytype = 0;
            } else if (txt == '微信支付') {
                paytype = 1;
            } else if (txt == '银联') {
                paytype = 2;
            } else if (txt == '余额支付') {
                paytype = 3;
            }else if (txt == '爱贝') {
                paytype = 4;
            } else {
                paytype = 0;
            }
     		console.log(paytype);
		


			var contentHtml='';
			
			var kindVal = <%=kind%>;
			
			var projectidVal="";
			var countNumVal="";
			var selectextraVal ="";
			
			
			if(kindVal==0){//详情处下单
				projectidVal='<%=projectid%>';
				countNumVal='<%=countNum%>';
				selectextraVal =decodeURIComponent('<%=selectextraVal%>');
			}else{//购物车处下单
				projectidVal=decodeURIComponent('<%=projectidArr%>');
				countNumVal=decodeURIComponent('<%=countNumArr%>');
				selectextraVal =decodeURIComponent('<%=guigeArr%>');
			}
	        
	        console.log(projectidVal);
	         console.log(countNumVal);
	         console.log(selectextraVal);
	        
         	var provCity = $.trim($("#demo2").val());
            var prov = provCity.split(",")[0];
            var city = provCity.split(",")[1];
            
            var provCityId = $.trim($("#value2").val());
            var shengidVal = provCityId.split(",")[0];
            var cityidVal = provCityId.split(",")[1];
           
            
            var username = $.trim($('#username').val());
            var usertel = $.trim($('#usertel').val());
            var detailAddress = $.trim($('#detailAddress').val());
            
			if (provCity=="") {
                showToast("你尚未选择省份和城市")
                return false;
            }
            if (usertel == "") {
                showToast("你还未填写联系电话");
                return false;
            }
            if (username == "") {
                showToast("你还未填写联系人");
                return false;
            }
            if (detailAddress == "") {
                showToast("你还未填写详细地址");
                return false;
            }
            if (Test_address(detailAddress)==false) {
                showToast("详细地址出现未知字符例如下划线等");
                return false;
            }
            // 组装地址 收货地址_联系人_电话（收货地址＝省+市+详细地址）
            var addressVal = prov + city + detailAddress + "_" + username + "_" + usertel;
            

        $.ajax({
            url: "PlugServlet",
            data: JSON.stringify({m: 1720, projectid: projectidVal, selectextra: selectextraVal,
                num: countNumVal, about: "", paytype: paytype, apptype: "0",address:addressVal,shengid:shengidVal,cityid:cityidVal}),
            type: 'post',
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            beforeSend: function () {
                showProgressDg();
            },
            success: function (data) {
              console.log(data);
                if (data.state == 1) {
                    if (paytype == 0) { // 支付宝
                        // TODO: 下单成功 掉起支付

                    } else if (paytype == 1) { // 微信
                        // TODO: 下单成功 掉起支付

                    } else if (paytype == 3) { // 余额
                        showTipDialog("提示", "支付成功，请在订单列表中查看订单信息！", function () {
                            javascript:history.go(-2);
                        }, 1);
                    } else if (paytype == 4) { // 爱贝
                        if(data['paytype']*1.0==4)
		           		{
		           		 	tempwindow.location=data['params'] ;
		           		}
                    }
                    else {
                        if(data['paytype']*1.0==2)
		           		{
		           		 	tempwindow.location=baseUrl+'pc/unionProPay.jsp?orderid='+data['orderid'] ;
		           		}
                    }
                } else {
                    // 其他错误弹出提示
                    showToast(data.info);
                }
                
                
                
                /* 新建或者打开数据库 */
		        websqlOpenDB(); 
		        websqlCreatTable(websqlTable);
		        
		        var idArr = projectidVal.split(",");
		        for(var i = 0;i<idArr.length;i++){
		        	/* 删除数据库数据 */
	                websqlDeleteADataFromTable(websqlTable,idArr[i]); 
		        }
		        
            },
            error: function (data) {
                showToast(data.info);
            },
            complete: function () {
                closeProgressDg();
            }
        });
       
      
        
    }

</script>
</html>


